package com.ticket;

import javax.xml.ws.Response;
import java.io.InputStream;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;

public class ChatService {
    private Map<String,String> questionMap;
    public  ChatService(){
        questionMap=new HashMap<>();
        questionMap.put("时间","请输入要查看时间的场次：(yyyy-MM-dd)");
        questionMap.put("地点","当前所在地点：");
        questionMap.put("退出","好的，再见！");
        questionMap.put("门票","请输入要看的明星：");
    }


    public  String Response(String question){
        for (String key:questionMap.keySet()){
            if(question.contains(key)){
                return questionMap.get(key);
            }
        }
        return null;
    }

    public static void Question() {
        ChatService service=new ChatService();
        getConnection getconnection = new getConnection();
        Scanner scanner = new Scanner(System.in);
        System.out.println("您想要问啥呀~：（输入“退出”结束问答哦）");
        System.out.println("包含关键字（门票   时间   地点   退出）");
        String question = scanner.nextLine();
        while(true) {
            String response="";
            String key="";
            if ((question.contains("门票")||question.contains("时间")||
                        question.contains("地点"))){
                    response = service.Response(question);
                     if(question.equals("门票")){
                         key="name";
                     } else if (question.contains("时间")) {
                         key="date";
                     }else{
                         key="address";
                     }
                if (response!=null){
                        System.out.println(response);
                        question=scanner.nextLine();
                        int result=getconnection.cc(question,key);
                        if (result==1){
                            break;
                        }else{
                            System.out.println("还想问什么呀");
                            question=scanner.nextLine();
                        }
                    }
                }
            else if (question.contains("退出")) {
                System.out.println("下次见！拜拜~");
                break;
            }
            else{
                    System.out.println("您的问题我还问答不了哦，换个问题吧~（不好意思啦）");
                    question=scanner.nextLine();
            }
        }
        System.out.println("-----------------------------------");
    }
}


class getConnection {

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    String qdate ;


    public int cc(String question,String key) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<String> list=new ArrayList<>();
        Scanner scanner=new Scanner(System.in);
        boolean temp=false;

        try {

            connection = DBUtils.getConnection();

            String sql = "select * from concert";
            preparedStatement = connection.prepareStatement(sql);
            resultSet=preparedStatement.executeQuery();
            //处理查询结果集
            while (resultSet.next()) {

                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                Date tdate = resultSet.getDate("date");
                qdate = formatter.format(tdate);

                String address = resultSet.getString("address");
                int total = resultSet.getInt("total");
                int number = resultSet.getInt("number");
                double price= resultSet.getDouble("price");

                if(question.contains(name)&&key.equals("name")){
                    String sql1 = "select number from concert where name=?";
                    preparedStatement = connection.prepareStatement(sql1);
                    preparedStatement.setString(1,name);
                    resultSet=preparedStatement.executeQuery();
                    while (resultSet.next()){
                        number = resultSet.getInt("number");
                    }
                    if(number==0){
                        System.out.println("已经没有票啦~\n");
                        temp=true;
                        break;
                    }else{
                       System.out.println(name+"的门票数量还剩："+number+"\t每张票"+price+"元\n");
                        temp=true;
                        break;
                       }
                } else if (question.contains(address)&&key.equals("address")) {
                    String sql2 = "select name from concert where address=?";
                    preparedStatement = connection.prepareStatement(sql2);
                    preparedStatement.setString(1,address);
                    resultSet=preparedStatement.executeQuery();
                    while (resultSet.next()){
                        name = resultSet.getString("name");
                        list.add(name);
                    }
                        System.out.println(address + "的场次有:\t" + list+"\n");
                        temp=true;
                        break;

                } else if(question.contains(qdate)&&key.equals("date")){
                    String sql3 = "select name from concert where date=?";
                    preparedStatement = connection.prepareStatement(sql3);
                    preparedStatement.setString(1,qdate);
                    resultSet=preparedStatement.executeQuery();
                    while (resultSet.next()){
                        name = resultSet.getString("name");
                        list.add(name);
                    }
                    System.out.println(tdate + "的场次有\t" + list+"\n");
                    temp=true;
                    break;

                }

            }
            if (!temp){
                if(question.matches("\\d{4}-\\d{2}-\\d{2}")){
                    System.out.println("这个时间没有场次");
                    return 0;
                }
                System.out.println("没有演唱会场次哦，换一个吧~");
                System.out.println("也可能是输入规范的问题，要按提示精准输入哦");
                question=scanner.nextLine();
                if (question.contains("退出")){
                    System.out.println("下次见！拜拜~");
                    return 1;
                }else{
                    cc(question,key);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtils.close(resultSet, preparedStatement, connection);
        }
        return 0;
    }
}

